package org.example;

public class algorithm_Demo3 {
    public static void main(String[] args) {
        /*//冒泡排序
        int[] arr = {1, 4, 2, 5, 3};
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
        //选择排序
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        //插入排序
        int startIndex=-1;
        for (int i = 0; i < arr.length-1; i++) {
            if (arr[i] > arr[i + 1]) {
                startIndex = i + 1;
                break;
            }
        }
        for (int i=startIndex;i<arr.length;i++) {
            int j=i;
            while(j>0&&arr[j]<arr[j-1]){
                int temp=arr[j];
                arr[j]=arr[j-1];
                arr[j-1]=temp;
                j--;
            }
        }
        System.out.println();
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }*/
        //递归算法
        //找出口 1的阶乘为1 方法不再调用自己
        System.out.println();
        //计算1-100的和
        System.out.println(getSum(100));
        //计算5的阶乘
        System.out.println("5的阶乘为"+getSum2(5));
    }
    public static int getSum(int number) {
        if(number==1) {
            return 1;
        }
        return number+getSum(number-1);
    }
    public static int getSum2(int number) {
        if(number==1) {
            return 1;
        }
        return number*getSum2(number-1);
    }
}